package com.ktjy.ch15.controller;


import com.ktjy.ch15.entity.Computerroom;
import com.ktjy.ch15.entity.Flower;
import com.ktjy.ch15.service.FlowerService;
import com.ktjy.ch15.utils.Constants;
import com.ktjy.ch15.utils.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main/flower")
public class FlowerController extends BaseController{
    private static final Logger LOGGER =
            LoggerFactory.getLogger(FlowerController.class);
    @Autowired
    private FlowerService flowerService;

    @RequestMapping("/page")
    public String page(Model model, Flower condition,
                       @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        Page<Flower> page = null;
        try {
            // 查询用户的分页信息
            page = flowerService.findPage(condition, pageNo, pageSize);
            // 将分页数据、角色列表和查询条件设置到模型数据中
            model.addAttribute("pageInfo", page);
            model.addAttribute("condition", condition);
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 page 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/page";
    }

    @GetMapping("/save")
    public String toSave(Model model) {
        try {
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("flower", new Flower());
        } catch (Exception e) {
            LOGGER.error("SysUserController中的toSave方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "flower/save";
    }

    @PostMapping("/save")
    public String doSave(Flower flower,
                         HttpSession session,
                         HttpServletRequest request) {
        try {

            boolean ok =false;
            if(flower.getFlowerId()==null){//新增
                ok = flowerService.save(flower);
            }else{//修改
                ok=flowerService.update(flower);
            }

            if(ok){
                //跳转到列表;重定向到分页方法
                return redirect("/main/flower/page");
            }else{
                //跳回新增页面
                return "flower/save";
            }
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 save 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/view/{flowerId}")
    public String view(@PathVariable("flowerId") Integer flowerId, Model model){
        Flower flower = null;
        try {
            flower = flowerService.findById(flowerId);
            model.addAttribute("flower", flower);
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 view 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/view";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("flowerId") Integer flowerId, Model model) {
        Flower flower = null;
        List<Flower> roleList = null;
        try {
            flower = flowerService.findById(flowerId);
            // 回显数据时需要 sysUser，这里给个空对象避免出错
            model.addAttribute("flower", flower);
        } catch (Exception e) {
            LOGGER.error("SysUserController 中的 toEdit 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "flower/save";
    }

    @DeleteMapping("/del/{flowerId}")
    @ResponseBody
    public Object del(@PathVariable("flowerId") Integer flowerId) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            boolean ok = flowerService.delete(flowerId);
            if (ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else {
                map.put("code",500);
                map.put("msg","删除失败");
            }
        }catch (Exception e){
            LOGGER.info("发生错误",e);
            map.put("code",500);
            map.put("msg","发生异常");
        }
        return map;
    }

    @GetMapping("/accountExist")
    @ResponseBody
    public Object accountExist(@RequestParam("flowerName") String flowerName) {
        Map<String,Object> map = new HashMap<>();
        try {
            Flower flower = flowerService.findByFlowerName(flowerName);
            if (flower != null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e){
            LOGGER.error("SysUserController中的accountExists方法执行发生错误", e);
            map.put("exist",-1);
            map.put("error", "出现异常，请联系管理员处理！ ");
        }
        return map;
    }
}
